$(() => {
    let $btn1 = $("#btn1");
    let $btn2 = $("#btn2");
    let $btn3 = $("#btn3");
    let $username = $("#username");
    let $password = $("#password");
    $(".sch").removeAttr("href").click(()=>{
        dialog("提示","经过我们的长期努力，最终我们取消了这个功能。");
    });
    $btn1.click(()=>{
        location.href="{LINK_register}";
    });
    $btn2.click(()=>{
        location.href="{LINK_forget}";
    });
    $btn3.click(()=>{
        $("#main").addClass("el-loading");
        let username = $username.val();
        let password = $password.val();
        let autoLogin = $("#autoLogin")[0].checked?"yes":"no";
        if(username.length>=3){
            if(password.length>=6&&password.length<=25){
                if(grecapthcaIsCallbackVerify){
                    $.post("#",{
                        username:username,
                        password:password,
                        autoLogin:autoLogin
                    },text=>{
                        $("#main").removeClass("el-loading");
                        let data=JSON.parse(text);
                        if(data.status){
                            dialog("提示",data.msg);
                            if(data.setup=="yes"){
                                if(callback.length>0){
                                    console.log(data);
                                    jumpToCallback(data.username,data.nickname,data.email,data.avatar,autoLogin);
                                }else{
                                    setTimeout(() => {
                                        location.href="{LINK_user}";
                                    }, 1000);
                                }
                            }else{
                                setTimeout(() => {
                                    location.href="{LINK_setup}";
                                }, 1000);
                            }
                        }else{
                            dialog("错误","登录失败："+data.msg);
                        }
                    });
                }else{
                    $("#main").removeClass("el-loading");
                    dialog("错误","请点击人机身份验证");
                }
            }else{
                $("#main").removeClass("el-loading");
                dialog("错误","密码必须为6-25个字符");
            }
        }else{
            $("#main").removeClass("el-loading");
            dialog("错误","用户名必须为大于3个字符")
        }
    });
    if(web_logo.length>0){
        $("#web_logo").attr("src",web_logo);
    }else{
        $("#web_logo").hide();
    }
    if($.cookie("isLogin")==="yes"){
        $("#loginUser").text($.cookie("username"));
        $("#onekey").show();
        $("#main").hide();
    }
    if(callback.length>0){
        let web_link = callback+"/";
        web_link = web_link.replace("http://","").replace("https://","");
        web_link = web_link.substr(0,web_link.indexOf("/"));
        $("#web_link").text(web_link);
        $("#auth").show();
    }

    $("#onekeyLogin").click(()=>{
        let username = $.cookie("username");
        let password = $.cookie("password");
        $.post("#",{
            username:username,
            password:password
        },text=>{
            let data=JSON.parse(text);
            if(data.status){
                if(callback.length>0){
                    jumpToCallback(data.username,data.nickname,data.email,data.avatar,"no");
                }else{
                    location.href="{LINK_user}";
                }
            }else{
                alert("登录失败："+data.msg);
            }
        })
    });

    $("#changeLogin").click(()=>{
        $.cookie("isLogin","");
        $.cookie("username","");
        $.cookie("password","");
        location.reload();
    });

    function jumpToCallback(username,nickname,email,avatar,autoLogin){
        let $jumpForm = $("#jumpForm");
        $jumpForm.attr("action",callback);
        $("#jumpUsername").val(username);
        $("#jumpNickname").val(nickname);
        $("#jumpEmail").val(email);
        $("#jumpAvatar").val(avatar);
        $("#jumpAutoLogin").val(autoLogin);
        $jumpForm.submit();
        console.log(arguments);
    }
});
let grecapthcaIsCallbackVerify;
function recaptchaCallback(token){
    grecapthcaIsCallbackVerify=token;
}